//
//  VisitTypeSelectViewcontroller.m
//  Marketing
//
//  Created by User on 16/5/19.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "VisitTypeSelectViewcontroller.h"
#define CELLHEIGHT4 45.0f

@interface VisitTypeSelectViewcontroller ()

@end
NSNotificationCenter *notifica;
UIButton *endbutt;
NSMutableArray *buttArr;
int tag;

@implementation VisitTypeSelectViewcontroller

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"拜访类型"];
    
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    self.view.backgroundColor = graySectionColor;
    
    notifica = [NSNotificationCenter defaultCenter];
    buttArr = [[NSMutableArray alloc] init];
    [self creatView];
}

- (void)creatView
{
    UILabel *signLab = [[UILabel alloc] initWithFrame:CGRectMake(LEFTWIDTH, 20, 80, 20)];
    signLab.text = @"请选择";
    signLab.textColor = grayFontColor;
    [ViewTool setLableFont14:signLab];
    [self.view addSubview:signLab];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, signLab.maxY , KSCreenW, 1)];
    line1.backgroundColor = grayLineColor;
    [self.view addSubview:line1];
    
    _dataArr = [NSMutableArray arrayWithObjects:@"日常拜访",@"项目拜访",@"紧急拜访",@"会议拜访", nil];
    
    UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, line1.maxY , KSCreenW, CELLHEIGHT4*4)];
    bgview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgview];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCreenW, 1)];
    line.backgroundColor = grayLineColor;
    [self.view addSubview:bgview];
    
    for (int i = 0; i < _dataArr.count; i ++) {
        UILabel * label= [[UILabel alloc] initWithFrame:CGRectMake(LEFTWIDTH,line1.maxY + 12.5 + CELLHEIGHT4*i , 90, 20)];
        label.text = [_dataArr objectAtIndex:i];
        label.textColor = blackFontColor;
        
        label.font = [UIFont systemFontOfSize:15.0f];
        [self.view addSubview:label];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(LEFTWIDTH, label.maxY+12.5, KSCreenW, 1)];
        line.backgroundColor = grayLineColor;
        if (i == _dataArr.count -1) {
            line.frame = CGRectMake(0, label.maxY+12.5, KSCreenW, 1);
        }
        [self.view addSubview:line];
        
        UIButton *butt = [[UIButton alloc] initWithFrame:CGRectMake(KSCreenW - LEFTWIDTH - 40,44.5+ 7+CELLHEIGHT4*i, 36, 36)];
        butt.center = CGPointMake(KSCreenW - 35, label.center.y);
        butt.tag = i;
        [butt setImage:[UIImage imageNamed:@"HY_list_btn_checkbox_normal"] forState:UIControlStateNormal];
        [butt setImage:[UIImage imageNamed:@"HY_list_btn_checkbox_selected"] forState:UIControlStateSelected];
        [butt addTarget:self action:@selector(selectedClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:butt];
        [buttArr addObject:butt];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(label.maxX,40+CELLHEIGHT4*i, KSCreenW-label.maxX-50,CELLHEIGHT4)];
        button.tag = i;

        [button addTarget:self action:@selector(selectedClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
}

- (void)selectedClick:(UIButton *)button
{
    UIButton *butt = [buttArr objectAtIndex:button.tag];
    
    if (endbutt != butt) {
        endbutt.selected = NO;
        butt.selected = YES;
        
        endbutt = butt;
    }else{
        butt.selected = !butt.selected;
    }
    if (butt.selected == YES) {
        tag = (int)butt.tag;
    }else{
        tag = 4;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self goToBack];
        
    });
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSDictionary *dic = @{@"visittype":@(4)};
    for (UIButton *butt in buttArr) {
        if (butt.selected == YES) {
            dic = @{@"visittype":@(tag)};
        }
    }
    
    [notifica postNotificationName:@"visittypeselect" object:nil userInfo:dic];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
